// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace BlueOnionSoftware.Bloget
{
    internal class PasswordView : View
    {
        private bool _disposed;
        private readonly Login _login = new Login();

        internal PasswordView(Bloget blogetArg)
            : base(blogetArg)
        {
            _login.ID = "login";
        }

        protected override string RootClassId()
        {
            return "password-view";
        }

        protected override void ShowView()
        {
            Bloget.Controls.Add(_login);
            Bloget.Load += OnLoad;
            _login.LoggedIn += LoginLoggedIn;
        }

        private void OnLoad(object sender, EventArgs ea)
        {
            _login.Focus();
            Bloget.Load -= OnLoad;
        }

        private void LoginLoggedIn(object sender, EventArgs e)
        {
            FormsAuthentication.RedirectFromLoginPage(_login.UserName, _login.RememberMeSet);
        }

        protected override void Dispose(bool managed)
        {
            try
            {
                if (_disposed == false)
                {
                    _disposed = true;

                    if (managed)
                    {
                        _login.Dispose();
                    }
                }
            }

            finally
            {
                base.Dispose(managed);
            }
        }
    }
}